---@class UIPlayerChangeName : UIBase
UIPlayerChangeName = UIBase.New(PanelNames.UIPlayerChangeName)
local t = UIPlayerChangeName

function t.Init()
    UIBase.Init(t)
end
local randomTitleList = nil
local randomNameList = nil
function t:OnCreate()
    self.changeSex = self:GetController("c1")

    self.ctrlModifyCount = self:GetController("fristTime")
    self.btnChangeName = self:GetChild("start");
    self.changeNameNeedMoney = self:GetChild("money")
    self.inputName = self:GetChild("inputName");
    self.closeChangeName = self:GetChild("black")
    self.btnRandom = self:GetChild("btnRandom")
    self.changeSex.selectedIndex = 0
    self:RegisterOnClick(self.btnChangeName, self.GoChangeName)
    self:RegisterOnClick(self.closeChangeName, self.ClickClose)
    self:RegisterOnClick(self.btnRandom,self.ClickRandom)
    self.titleNum = 0
    self.nameNum = 0
    if randomTitleList == nil then
        randomTitleList = {}
        for k,v in pairs(G.dataTable["t_randomtitle"]) do
            table.insert(randomTitleList,v)
        end
    end
    if randomNameList == nil then
        randomNameList = {}
        for k,v in pairs(G.dataTable["t_randomname"]) do
            table.insert(randomNameList,v)
        end
    end
    self.titleNum = #randomTitleList
    self.nameNum = #randomNameList
    local seed = tostring(os.time()):reverse()
    math.randomseed(tonumber(seed))
    self:ShowChangeName()
end

function t:OnEnable()
    if logicMgr.HeroManager.GetRoleInfo("modifyCount") >= 1 then
        self.inputName.text = logicMgr.HeroManager.GetRoleInfo("name");
        self.ctrlModifyCount.selectedIndex = 1;
    else
        self.inputName.text = ""
        self.ctrlModifyCount.selectedIndex = 0;
        self:ClickRandom()
    end

    local money = logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.GOLD);

    if money >= 100 then
        self.changeNameNeedMoney.text = string.TextColor("100", "#FFFFFF");
    else
        self.changeNameNeedMoney.text = string.TextColor("100", "#FF0000");
    end
    logicMgr.GuideMgr.SoftPause()
end
function t:OnDisable()
end
function t:OnDestroy()
end

--- 显示/隐藏 名称修改界面
function t:ShowChangeName()
    self.changeSex.selectedIndex = logicMgr.HeroManager.GetRoleInfo("sex")

    self.ctrlModifyCount.selectedIndex = 0
    if logicMgr.HeroManager.GetRoleInfo("modifyCount") >= 1 then
        self.ctrlModifyCount.selectedIndex = 1
        self.inputName.text = logicMgr.HeroManager.GetRoleInfo("name");
    end
end
function t:ClickRandom()
    local index = math.random(1, self.titleNum)
    local indexName = math.random(1, self.nameNum)
    local surname = ""
    local name = ""
    local cfgTitle =  randomTitleList[index]
    if cfgTitle then
        surname = cfgTitle["f_Title"]
    end
    local cfgName = randomNameList[indexName]
    if cfgName then
        name = cfgName["f_Name"]
    end
    local randomName = surname..name
    self.inputName.text = randomName
    if logicMgr.ChatManager.HasFilterBlockWord(randomName) then
        self:ClickRandom()
    end
end
function t:GoChangeName()
    local newName = self.inputName.text
    local money = logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.GOLD)
    if logicMgr.HeroManager.GetRoleInfo("modifyCount") >= 1 and money < HelperFunc.GetGlobalNumVlue(92) then
        UISysTips.AddMsg(getLanguage("ErrorCode_3412"));
        return
    end

    if #newName == 0 or newName == "" then
        UISysTips.AddMsg("名字不能为空");
        return
    end
    if tonumber(newName) ~= nil then
        UISysTips.AddMsg("名字不能为纯数字");
        return
    end
    if logicMgr.ChatManager.HasFilterBlockWord(newName) then
        UISysTips.AddMsg(getLanguage("UIFriend_ApplyMsgBlock"))
        return
    end
    --请求改名
    if newName == logicMgr.HeroManager.GetRoleInfo("name") and logicMgr.HeroManager.GetRoleInfo("sex") == self.changeSex.selectedIndex then
        self:ClickClose()
        UISysTips.AddMsg(getLanguage("Sys_ChangeNameNoChange"));
    else
        roleInfoHandler.R_ModifyPerson(newName, self.changeSex.selectedIndex)
        self:ClickClose()
    end

end
function t:ClickClose()
    closeUI(PanelNames.UIPlayerChangeName)
    logicMgr.GuideMgr.SoftResume()
end
return t